package cn.onein.edu.manage.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.onein.edu.manage.domain.LearnCategory;
import cn.onein.edu.manage.domain.LearnContent;
import cn.onein.edu.manage.domain.LearnSection;
import cn.onein.edu.manage.service.LearnContentService;

@Controller
public class LearnContentController {

	@Autowired
	private LearnContentService learnContentService;

	@RequestMapping(value = "/learnContent/getCategory.htm", method = RequestMethod.POST)
	public void getCategory(Long sectionId, HttpServletResponse resp,
			HttpServletRequest req) throws Exception {
		resp.setContentType("text/html");
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");

		LearnSection section = new LearnSection();
		StringBuffer buffer = new StringBuffer();
		section.setId(sectionId);
		List<LearnCategory> categorys = learnContentService
				.findCategorysBySection(section);
		for (LearnCategory category : categorys) {
			buffer.append("<option value=" + category.getId() + " >"
					+ category.getTitle() + "&nbsp;&nbsp;&nbsp;&nbsp;" + category.getContents().size() + "</option>");
		}
		resp.getWriter().write(buffer.toString());

	}

	@RequestMapping(value = "/learnContent/init.htm", method = RequestMethod.GET)
	public ModelAndView init(HttpServletResponse resp, HttpServletRequest req)
			throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		List<LearnSection> list = learnContentService.findSection();
		model.put("sections", list);

		return new ModelAndView("addContent", model);
	}

	@RequestMapping(value = "/learnContent/add.htm", method = RequestMethod.POST)
	public ModelAndView add(LearnContent content, Long categoryId,
			HttpServletResponse resp, HttpServletRequest req) throws Exception {
		resp.setContentType("text/html");
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String sectionId = req.getParameter("section");

		LearnCategory category = new LearnCategory();
		category.setId(categoryId);
		content.setCategory(category);
		content.setEnable(Boolean.TRUE);
		content.setCreateDate(new Date());
		content.setCreateBy("learnadmin");
		learnContentService.save(content);

		LearnSection section = new LearnSection();
		StringBuffer buffer = new StringBuffer();
		section.setId(Long.valueOf(sectionId));
		List<LearnCategory> categorys = learnContentService
				.findCategorysBySection(section);
		for (LearnCategory o : categorys) {
			if (categoryId.toString().equals(o.getId().toString())) {
				buffer.append("<option value=" + o.getId() + " selected >"
						+ o.getTitle() + "&nbsp;&nbsp;&nbsp;&nbsp;" + o.getContents().size() + "</option>");
			} else {
				buffer.append("<option value=" + o.getId() + " >"
						+ o.getTitle() + "&nbsp;&nbsp;&nbsp;&nbsp;" + o.getContents().size() + "</option>");
			}
		}

		req.setAttribute("selSection", sectionId);
		req.setAttribute("selCategory", buffer.toString());
		req.setAttribute("flag", "success");

		return init(resp, req);
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

}
